package com.ruoyi.project.business.tenantsocialsecurity.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.tenantsocialsecurity.entity.TenantsocialSecurity;
import com.ruoyi.project.business.tenantsocialsecurity.mapper.TenantsocialSecurityMapper;
import com.ruoyi.project.business.tenantsocialsecurity.request.AddTenantsocialSecurityRequest;
import com.ruoyi.project.business.tenantsocialsecurity.request.ListTenantsocialSecurityRequest;
import com.ruoyi.project.business.tenantsocialsecurity.request.UpdateTenantsocialSecurityRequest;
import com.ruoyi.project.business.tenantsocialsecurity.response.TenantsocialSecurityResponse;
import com.ruoyi.project.business.tenantsocialsecurity.service.ITenantsocialSecurityService;
import com.ruoyi.project.business.utils.BusinessUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 社保基本配置Service业务层处理
 * 
 * @author ${author}
 * @date 2024-03-19
 */
@Service
public class TenantsocialSecurityServiceImpl implements ITenantsocialSecurityService {
    @Autowired
    private TenantsocialSecurityMapper tenantsocialSecurityMapper;

    /**
     * 查询社保基本配置
     * 
     * @return 社保基本配置
     */
    @Override
    public TenantsocialSecurity selectTenantsocialSecurityById(Long id) {
        return tenantsocialSecurityMapper.selectTenantsocialSecurityById(id);
    }

    /**
     * 查询社保基本配置列表
     * 
     * @return 社保基本配置
     */
    @Override
    public List<TenantsocialSecurityResponse> selectTenantsocialSecurityList(ListTenantsocialSecurityRequest request, Long tenantId) {


        TenantsocialSecurity param = BeanUtils.copy(request, TenantsocialSecurity.class);
        param.setTenantId(tenantId);
        Date yearMonth = request.getYearMonth();
        Integer year = DateUtils.year(yearMonth);
        Integer month = DateUtils.month(yearMonth);
        param.setYear(year);
        param.setMonth(month);
        List<TenantsocialSecurity> entitis = tenantsocialSecurityMapper.selectTenantsocialSecurityList(param);
        List<TenantsocialSecurityResponse> resList = BeanUtils.copyList(entitis, TenantsocialSecurityResponse.class);

        BusinessUtil.fillListNames(resList, tenantId);
        // TODO 填充 name

        return resList;
    }

    /**
     * 新增社保基本配置
     * 
     * @return 结果
     */
    @Override
    public int insertTenantsocialSecurity(AddTenantsocialSecurityRequest request, Long operatorId, Long tenantId) {
        TenantsocialSecurity entity = BeanUtils.copy(request, TenantsocialSecurity.class);
        Date yearMonth = request.getYearMonth();
        Integer year = DateUtils.year(yearMonth);
        Integer month = DateUtils.month(yearMonth);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setCreator(operatorId);
        entity.setDeleted(Boolean.FALSE);
        entity.setCreateTime(DateUtils.getNowDate());
        return tenantsocialSecurityMapper.insertTenantsocialSecurity(entity);
    }

    /**
     * 修改社保基本配置
     * 
     * @return 结果
     */
    @Override
    public int updateTenantsocialSecurity(UpdateTenantsocialSecurityRequest request, Long operatorId, Long tenantId) {
        TenantsocialSecurity entity = BeanUtils.copy(request, TenantsocialSecurity.class);
        Date yearMonth = request.getYearMonth();
        Integer year = DateUtils.year(yearMonth);
        Integer month = DateUtils.month(yearMonth);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setUpdateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setUpdater(operatorId);
        return tenantsocialSecurityMapper.updateTenantsocialSecurity(entity);
    }

    /**
     * 批量删除社保基本配置
     * 
     * @return 结果
     */
    @Override
    public int deleteTenantsocialSecurityByIds(Long[] ids, Long operatorId, Long tenantId) {
        return tenantsocialSecurityMapper.deleteTenantsocialSecurityByIds(ids);
    }

    /**
     * 删除社保基本配置信息
     * 
     * @return 结果
     */
    @Override
    public int deleteTenantsocialSecurityById(Long id, Long operatorId, Long tenantId) {
        return tenantsocialSecurityMapper.deleteTenantsocialSecurityById(id);
    }

    @Override
    public TenantsocialSecurity selectByUser(int year, Long userId, Long tenantId) {
        TenantsocialSecurity param = new TenantsocialSecurity();
        param.setTenantId(tenantId);
        param.setYear(year);
        param.setUserId(userId);
        List<TenantsocialSecurity> tenantsocialSecurities = tenantsocialSecurityMapper.selectTenantsocialSecurityList(param);
        // 理论上只有一条
        if(CollectionUtils.isNotEmpty(tenantsocialSecurities)) {
            return tenantsocialSecurities.get(0);
        }
        return null;
    }
}
